package com.ftg.learn.day13;

import org.apache.ibatis.binding.MapperRegistry;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * socker  server
 *
 * @author 布衣
 */
public class SocketServer {

    public static void main(String[] args) {
        InputStream inputStream = null;
        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(9999);
            while (true) {
                socket = serverSocket.accept();
                inputStream = socket.getInputStream();
                byte[] bytes = new byte[1024];
                int n;
                StringBuilder sb = new StringBuilder();
                while ((n = inputStream.read(bytes)) != -1) {
                    sb.append(new String(bytes, 0, n, StandardCharsets.UTF_8));
                }
                System.out.println("==>" + sb);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (null == inputStream) {
                try {
                    inputStream.close();
                    socket.close();
                    serverSocket.close();
                } catch (IOException io) {
                    io.printStackTrace();
                }
            }

        }

    }

}
